<?php
namespace Api\Controller;
use Think\Controller;
class PayController extends CommonController {
    public function _initialize(){
		
	}
	
	//更新商品金额
	public function totalprice(){
		$cart_num	= I('post.cart_num');
		$totalprice	= I('post.total_price');
		$ware_id	= I('post.ware_id');
		$cart_list  = I('post.cart_list');
		$order_id 	= I('post.order_id');
		
		$Shopcar = M('Ware_shopcar');
		$res = $Shopcar->where("ware_id={$ware_id} and cart_list='{$cart_list}' and order_id=0")->find();
		$data = array();
		if( $res == false){
			$res = $Shopcar->where("ware_id={$ware_id} and cart_list='{$cart_list}' and order_id={$order_id}")->find();
			
			$data = array(
				'totalprice'	=>	$totalprice,
				'cart_num'		=>	$cart_num
			);
			$res = $Shopcar->where("ware_id={$ware_id} and cart_list='{$cart_list}' and order_id={$order_id}")->save($data);
			$this->response( true , 0 , '更新成功' , true);
		}
		else{
			$data = array(
				'totalprice'	=>	$totalprice,
				'cart_num'		=>	$cart_num
			);
			$res = $Shopcar->where("ware_id={$ware_id} and cart_list='{$cart_list}' and order_id=0")->save($data);
			$this->response( true , 0 , '更新成功' , true);
		}
		
	}
	
	//购物车结算时检查是否登录并把购买数据存到数据库
	public function logincheck(){
		$payprice = I('get.payprice');
	
		if( isset($_SESSION['uid']) && $_SESSION['uid'] !='' 
			&& isset($_SESSION['name']) && $_SESSION['name'] !='')
		{
			$Shopcar = M('Ware_shopcar');
			$Pay = M('Ware_pay');
			
			$list = $Shopcar->where("order_id=0 and status=0")->select();
			if( $list == false){
				$this->response( false , 2 , '未勾选' , true);
			}
			$data = array();
			$paylist = array();//存入支付数据库
			
			$order_id = substr(date("ymdHi"),3,9).mt_rand(1,999);
			$data = array(
				'order_id'	=>	$order_id,
				'user_id'		=>	$_SESSION['uid']
			);
			$paylist = array(
				'user_id'		=>	$_SESSION['uid'],
				'order_id'		=>	$order_id,
				'status'		=>	1,						//待付款
				'addtime'		=>	time(),
				'payprice'		=>	$payprice
			);
			$res = $Shopcar->where("order_id=0")->save($data);
			$ret = $Pay->add($paylist);
			$this->response( true , 0 , $data ,true);
		}
		$this->response( false , 1 , '未登录' ,true);
	}
	//提交订单增加下单时间和邮费
	public function paybtn(){
		$tips		= I('post.tips');
		$order_id	= I('post.order_id');
		$payprice	= I('post.payprice');
		
		$Pay = M('Ware_pay');
		$data = array();
		
		$res = $Pay->where("order_id={$order_id}")->find();
		if( $res == false){
			$this->response( false , 1 , '不存在订单编号' , true);
		}
		$data = array(
			'addtime'	=>	time(),
			'tips'		=>	$tips,
			'payprice'	=>	$payprice,
			'status'	=>	2			//买家已付款
		);
		$res = $Pay->where("order_id={$order_id}")->save($data);
		$this->response( true , 0 , '已下单' , true);
	}
	
	//单击勾选改变状态
	/*public function clickchange(){
		$click_id = I('get.click_id');
		$status = I('get.status');
		
		$Shopcar = M('Ware_shopcar');
		$res = $Shopcar->where("id={$click_id}")->find();
		if( $res == false){
			$this->response( false , 0 , 'id不存在' , true);
		}
		$data = array(
			'status'	=>	$status
		);
		
		$res = $Shopcar->where("id={$click_id}")->save($data);
		$this->response( true , 0 , '修改成功' , true);
	}*/
	
}
